import csv
import datetime
import os
import re
from bs4 import BeautifulSoup
from lxml import etree

import requests
from retrying import retry


class Exam():
    def __init__(self, url):
        # 请求头
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Cookie': 'uuid=ebee1951-2dd6-440b-f8ad-e9ba0b03e284; cityDomain=bj; clueSourceCode=%2A%2300; user_city_id=12; ganji_uuid=4980507090916713294106; sessionid=258f4641-c7fc-467f-a707-fda9e0cb9047; lg=1; Hm_lvt_bf3ee5b290ce731c7a4ce7a617256354=1605140191,1605167505; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22self%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%22ebee1951-2dd6-440b-f8ad-e9ba0b03e284%22%2C%22ca_city%22%3A%22suqian%22%2C%22sessionid%22%3A%22258f4641-c7fc-467f-a707-fda9e0cb9047%22%7D; preTime=%7B%22last%22%3A1605170434%2C%22this%22%3A1605167506%2C%22pre%22%3A1605167506%7D; Hm_lpvt_bf3ee5b290ce731c7a4ce7a617256354=1605170433; rfnl=https://www.guazi.com/bj/buy/o3/; antipas=h8F112714E970736CF13488w',
            'Host': 'www.guazi.com',
            'Pragma': 'no-cache',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
        }
        self.url = url
        if input('是否设置递增(1)') == '1':
            self.num = int(input('初始值'))
            self.add = int(input('递增值'))
        else:
            self.num, self.add = None, None

    @retry(stop_max_attempt_number=3)
    def RTX(self):
        # 发送请求
        if self.num:
            self.r = requests.get(self.url % self.num, headers=self.headers)
            print('当前请求路径：', self.url % self.num)
            self.num += self.add
        else:
            self.r = requests.get(self.url, headers=self.headers)
            print('当前请求路径：', self.url)
        print(self.r.text)
        return True

    def RDX(self):
        # 获取数据
        car_ul = BeautifulSoup(self.r.content.decode('utf-8'), features='lxml').find_all(
            **{'class': 'carlist clearfix js-top'})
        car_li = BeautifulSoup(str(car_ul[0]), features='lxml').find_all('li')
        all_list = []
        for i in range(len(car_li)):
            one_car = etree.HTML(str(car_li[i]))[0]
            title = one_car.xpath('//h2/text()')[0]
            year = one_car.xpath('//div[@class="t-i"]/text()')[0]
            mileage = one_car.xpath('//div[@class="t-i"]/text()')[1]
            ruling_price = one_car.xpath('//p/text()')[0]
            original_price = one_car.xpath('//em[@class="line-through"]/text()')[0] if len(
                one_car.xpath('//em[@class="line-through"]/text()')) > 0 else None

            one_dict = {'title': title, 'year': year, 'mileage': mileage, 'ruling_price': ruling_price,
                        'original_price': original_price}
            all_list.append(one_dict)
        # 获取日期
        data = '%s%02d%02d' % (
            datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day)
        # 判断文件是否存在
        if os.path.exists('瓜子二手车_车辆信息_%s.csv' % data):
            open_choose = 'a'
            print('打开文件...open_choose_%s.csv' % data)
        else:
            open_choose = 'w'
            print('新建文件...open_choose_%s.csv' % data)
        # 打开文件
        with open('瓜子二手车_车辆信息_%s.csv' % data, '%s' % open_choose, encoding='utf-8') as f:
            print('打开文件：瓜子二手车_车辆信息_%s.csv' % data)
            c = csv.DictWriter(f, ['title', 'year', 'mileage', 'ruling_price', 'original_price'])
            if open_choose == 'w':
                c.writeheader()
            c.writerows(all_list)
            f.close()
            print('写入完成')


if __name__ == '__main__':
    a = Exam('https://www.guazi.com/bj/buy/o%s/#bread')
    while True:
        if a.RTX():
            a.RDX()
        if input('是否继续（1）') != '1':
            break
